package com.cardbaobao.cardbabyclient.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.util.AttributeSet;
import android.view.View;

/**
 * 圆点View
 * Created by zad
 * 2016/10/27 10:47
 */

public class CircleDotView extends View {

    private Paint mBgPaint;
    /**
     * 抗锯齿过滤器
     */
    private PaintFlagsDrawFilter pfdf;
    /**
     * 圆形半径
     */
    private int mViewRadius;


    public CircleDotView(Context context) {
        this(context,null);
    }

    public CircleDotView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public CircleDotView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mBgPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mBgPaint.setColor(Color.parseColor("#DDDDDD"));
        pfdf = new PaintFlagsDrawFilter(0,Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec,heightMeasureSpec);
        int measureWidth = getMeasuredWidth();
        int measureHeight = getMeasuredHeight();
        int size = Math.max(measureWidth,measureHeight);//圆形直径
        mViewRadius = size / 2;
        setMeasuredDimension(size,size);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.setDrawFilter(pfdf);
        canvas.translate(mViewRadius,mViewRadius);
        canvas.drawCircle(0,0,mViewRadius,mBgPaint);
    }

    @Override
    public void setBackgroundColor(int color) {
        mBgPaint.setColor(color);
        invalidate();
    }
}
